package cn.bdqn.layui.service.user;

import cn.bdqn.layui.entity.Role;
import cn.bdqn.layui.entity.User;
import cn.bdqn.layui.entity.vo.UserVO;
import cn.bdqn.layui.mapper.RoleMapper;
import cn.bdqn.layui.mapper.UserMapper;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserDetailsService, UserService {
    @Resource
    UserMapper userMapper;

    @Resource
    RoleMapper roleMapper;

    @Override
    public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
        User user = userMapper.findByUserName(userName, 0);
        if (user == null) {
            throw new UsernameNotFoundException("用户不存在");
        }
        user.setRoleList(userMapper.getRolesById(user.getId()));
        return user;
    }

    @Override
    public User findByUserName(String userName, Integer id) {
        return userMapper.findByUserName(userName, id);
    }

    @Override
    public List<Role> getRolesById(Integer uid) {
        return userMapper.getRolesById(uid);
    }

    @Override
    public List<User> findUserList(Integer pageNo, Integer pageSize, Integer id, String userName) {
        return userMapper.findUserList(pageNo, pageSize, id, userName);
    }

    @Override
    public int findUserCount(Integer id, String userName) {
        return userMapper.findUserCount(id, userName);
    }

    @Override
    public int addUser(UserVO userVO) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        User user = (User) authentication.getPrincipal();
        userVO.setPassword(new BCryptPasswordEncoder().encode(userVO.getPassword()));
        int i = 0;
        if (userVO.getOpen().equals("on")) {
            userVO.setEnabled(1);
            userVO.setLocked(1);
            userVO.setAccount(1);
            userVO.setMoney(BigDecimal.valueOf(2000.00));
            userVO.setIntegral(100);
            userVO.setCreationDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            userVO.setCreatedBy(user.getId());
            i = userMapper.addUser(userVO);
            for (String rid : userVO.getRoles()) {
                if (Integer.parseInt(rid) == 12) {
                    userVO.setBangkeStatic(0);
                    userMapper.updateUserBangkeStatus(0,userVO.getId());
                }
                userMapper.addUserRole(userVO.getId(), Integer.parseInt(rid));
            }
        }
        return i;
    }

    @Override
    public int addUserRole(Integer uid, Integer rid) {
        return userMapper.addUserRole(uid, rid);
    }

    @Override
    public int delUser(String id) {
        return userMapper.delUser(id);
    }

    @Override
    public int updateUser(UserVO userVO) {
        User user = userMapper.findByUserName(userVO.getUsername(), -1);
        int i = 0;
        List<UserVO> byUid = userMapper.findByUid(userVO.getId());
        if (user == null && userVO.getRoles().length > 0) {
            //用户修改
            for (UserVO userVO1 : byUid) {
                userMapper.deleteUserRole(userVO1.getUid());
            }
            i = userMapper.updateUser(userVO);
            if (i > 0) {
                for (String id : userVO.getRoles()) {
                    userMapper.addUserRole(userVO.getId(), Integer.parseInt(id));
                }
            }
        }
        if (user == null && userVO.getRoles().length == 0) {
            i = userMapper.updateUser(userVO);
        }
        if (user != null && userVO.getRoles().length >= 0) {
            for (UserVO userVO1 : byUid) {
                userMapper.deleteUserRole(userVO1.getUid());
            }
            for (String id : userVO.getRoles()) {
                userMapper.addUserRole(userVO.getId(), Integer.parseInt(id));
            }
        }
        return i;
    }

    @Override
    public List<UserVO> findByUid(Integer uid) {
        return userMapper.findByUid(uid);
    }

    @Override
    public int deleteUserRole(Integer id) {
        return userMapper.deleteUserRole(id);
    }

    @Override
    public List<UserVO> findByBangKe(Integer bangkeStatic) {
        return userMapper.findByBangKe(bangkeStatic);
    }

}
